﻿CREATE PROCEDURE [dbo].[RenameDatabase]
	@oldDbName NVARCHAR(200),
	@newDbName NVARCHAR(200)
AS
BEGIN
	DECLARE @query varchar(2000)

	IF EXISTS(SELECT * FROM master.dbo.sysdatabases WHERE name = @oldDbName) AND NOT EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = @newDbName)
	BEGIN
		SET @query = 'ALTER DATABASE [' + @oldDbName + '] MODIFY NAME = N''' + @newDbName + ''''
		EXEC (@query)
	END
	ELSE
	BEGIN
		RAISERROR (N'Database %s exists or not exist  %s. [dbo].[RenameDatabase] interrupted', 11, 1, @newDbName, @oldDbName);
	END
END